#include "ChatClientWindow.h"
#include "ui_ChatClientWindow.h"
#include <QThread>

ChatClientWindow::ChatClientWindow(/*MainWindow* m_mainwindow,*/ QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatClientWindow)
{
    ui->setupUi(this);
    // m_tcpClientThread = new QThread();
    m_tcpClient = new TCPClient();
    // m_tcpClient->moveToThread(m_tcpClientThread);
    // m_tcpClientThread->start();//开启单独线程
    m_tcpClient->connectToServer("127.0.0.1", 8090);

    // 设为独立窗口
    setWindowFlags(Qt::Window);

    connect(ui->Btn_Client_Close, SIGNAL(clicked()), this, SLOT(closeClient()));
    connect(ui->Btn_Client_Sending, SIGNAL(clicked()), this, SLOT(sendMessage()));
    connect(this, SIGNAL(clientClose()), m_tcpClient, SLOT(disconnectFromHost()));
    connect(this, SIGNAL(clientSendMessage(QString)), m_tcpClient, SLOT(sendMessage(QString)));
    connect(m_tcpClient, SIGNAL(clientRename(QString)), this, SLOT(clientRename(QString)));

}

ChatClientWindow::~ChatClientWindow()
{
    delete ui; // 释放 UI
    delete m_tcpClient;
}

void ChatClientWindow::closeClient()
{
    emit clientClose();
    this->close();
}

void ChatClientWindow::sendMessage()
{
    QString message = ui->Message_Input->text();
    emit clientSendMessage(name + ":" + message);
    ui->Message_Input->clear();
}

void ChatClientWindow::clientRename(QString _name)
{
    name = _name;
    setWindowTitle(name);
}
